import Vue from 'vue';
import axios from 'axios';
import LocalStorage from '../utils/local-storage';
// import store from '../store/index';

const service = axios.create({
  baseURL: process.env.BASE_API,
  // baseURL: 'http://api.lanchewuyou.cn/api/v1/',
  timeout: 5000,
});

//request拦截器
service.interceptors.request.use(
  config => {
    if (LocalStorage.getToken()) {
      config.headers['Authorization'] = LocalStorage.getToken();

    }

    // if (config.method === 'get' && typeof config.data === 'object') {
    //   const {data} = config;
    //   config.url += `?${
    //     Object.keys()
    //   }`
    // }

    return config;
  },
  error => {
    console.log(error);
    return Promise.reject(error);
  }
);


//response拦截器
service.interceptors.response.use(
  response => {
    if (response.status !== 200) {
      return Promise.reject('error');
    } else {
      return Promise.resolve(response.data);
    }
  },
  error => {
    return Promise.reject(error);
  }
);


// //封装get请求
// export function get(url, params = {}) {
//   return new Promise((resolve, reject) => {
//     service.get(url, {
//         params: params
//       })
//       .then(response => {
//         resolve(response.data);
//       })
//       .catch(error => {
//         reject(error);
//       })
//   })
// }

export default function request ({url, method='get', data}) {

  return service.request({
    url,
    method,
    data
  });
}
